package renguanyu.app.tools.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.handlers.MapListHandler;

import javafx.fxml.FXML;
import renguanyu.app.tools.model.TestUi;
import renguanyu.app.tools.util.DbUtil;
import renguanyu.app.tools.util.ThreadUtil;

public class TestUiController {

	@FXML
	public void openBrowser() {
		TestUi.openBrowser();
	}

	@FXML
	public void closeBrowser() {
		TestUi.closeBrowser();
	}

	@FXML
	public void cleanProcess() {
		TestUi.killProcess();
	}

	@FXML
	public void openIndex() {
		TestUi.openIndex();
	}

	@FXML
	public void inputCsdn() {
		TestUi.inputCsdn();
	}

	@FXML
	public void clickSearch() {
		TestUi.clickSearch();
	}

	@FXML
	public void runBaiduSearchCsdn() {
		new Thread(()->{
			TestUi.openBrowser();
			TestUi.waiting();
			TestUi.openIndex();
			TestUi.waiting();
			TestUi.inputCsdn();
			TestUi.waiting();
			TestUi.clickSearch();
			TestUi.waiting();
			TestUi.closeBrowser();
		}).start();
	}

	@FXML
	public void openYgdyIndex() {
		TestUi.openYgdy();
	}
	
	@FXML
	public void clickOumei() {
		TestUi.clickOumei();
	}

	@FXML public void switchOumei() {
		TestUi.switchWindow();
	}

	@FXML public void getOumeiHref() {
		TestUi.getOumeiHref();
	}

	@FXML public void getOumeiTotal() {
		TestUi.getOumeiTotal();
	}

	@FXML public void clickNextPage() {
		TestUi.clickNextPage();
	}

	@FXML public void isLastPage() {
		TestUi.isLastPage();
	}

	@FXML
	public void runYgdyOumeiP1() {
		new Thread(()->{
			TestUi.openBrowser();
			TestUi.openYgdy();
		}).start();
	}
	@FXML
	public void runYgdyOumeiP2() {
		new Thread(()->{
			TestUi.flushWindow();
		}).start();
	}
	@FXML
	public void runYgdyOumeiP3() {
		new Thread(()->{
			TestUi.clickOumei();
//			ThreadUtil.sleep(5);
			TestUi.getOumeiHref();
			while(true) {
				boolean lastPage = TestUi.isLastPage();
				if(lastPage) {
					break;
				}
				TestUi.clickNextPage();
				ThreadUtil.sleep(5);
				TestUi.getOumeiHref();
				ThreadUtil.sleep(5);
			}

		}).start();
	}

	@FXML public void flushWindow() {
		TestUi.flushWindow();
	}

	@FXML public List<Map<String,Object>> readDetailPage() {
		String sql = "select id,name,url from ygdy_movie_oumei;";
		List<Map<String, Object>> query = DbUtil.query(sql, new MapListHandler());
		System.out.println(query);
		return query;
		
	}
	@FXML public void openDetailPage() {
		List<Map<String, Object>> detailPage = readDetailPage();
		Map<String, Object> map = detailPage.get(0);
		String url = map.get("url").toString();
		
		TestUi.openDetailPage(url);
		
		
		
	}
	@FXML public void getDetailPage() {
		
		
		
	}
	
	
}
